﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using Dapper;
using Blog.IRepository;
using Blog.Model;
using System.Data.SqlClient;

namespace Blog.Repository
{
    public partial class ParentRepository : IParentRepository
    {
        public int Add(Parent model)
        {
            using (IDbConnection conn=new SqlConnection(BaseDBConfig.ConnectionString)) {
               return conn.Execute("insert into Parent(Pname) values(@Pname)", model);
            }
        }

        public int Delete(Parent model)
        {
            using (IDbConnection conn = new SqlConnection(BaseDBConfig.ConnectionString))
            {
                return conn.Execute("delete from Parent where Parentid=@Parentid", model);
            }
        }

        public List<Parent> GetModel(Expression<Func<Parent, bool>> whereExpression)
        {
            using (IDbConnection conn = new SqlConnection(BaseDBConfig.ConnectionString))
            {
                return conn.Query<Parent>("Select *From Parent").AsQueryable().Where(whereExpression).ToList();
            }
        }

        public int Update(Parent model)
        {

            using (IDbConnection connection = new SqlConnection(BaseDBConfig.ConnectionString))
            {
                return connection.Execute("update Parent set Pname=@Pname where Parentid=@Parentid", model);
            }
        }
    }

    public partial class ArticleRepository : IArticleRepository
    {
        public int Add(Article model)
        {
            using (IDbConnection conn = new SqlConnection(BaseDBConfig.ConnectionString))
            {
                return conn.Execute("insert into Article(CategoryId,Adminid,Source,Author,Intro,[Content],Title,Hits,ReleaseDate,UpdateDate,CommentStatus,HelperNum,NoHelperNum,Keyword,Recommend)" +
                    " values(@CategoryId,@Adminid,@Source,@Author,@Intro,@Content,@Title,@Hits,@ReleaseDate,@UpdateDate,@CommentStatus,@HelperNum,@NoHelperNum,@Keyword,@Recommend)", model);
            }
        }

        public int Delete(Article model)
        {
            using (IDbConnection conn = new SqlConnection(BaseDBConfig.ConnectionString))
            {
                return conn.Execute("delete from Article where Articleid=@Articleid", model);
            }
        }

        public List<Article> GetModel(Expression<Func<Article, bool>> whereExpression)
        {
            using (IDbConnection conn = new SqlConnection(BaseDBConfig.ConnectionString))
            {
                return conn.Query<Article>("Select *From Article").AsQueryable().Where(whereExpression).ToList();
            }
        }

        public int Update(Article model)
        {

            using (IDbConnection connection = new SqlConnection(BaseDBConfig.ConnectionString))
            {
                return connection.Execute("update Article set CategoryId=@CategoryId,Adminid=@Adminid,Source=@Source," +
                    "Author=@Author,Intro=@Intro,[Content]=@Content,Title=@Title,Hits=@Hits,ReleaseDate=@ReleaseDate,UpdateDate=@UpdateDate,CommentStatus=@CommentStatus,HelperNum=@HelperNum,NoHelperNum=@NoHelperNum,Keyword=@Keyword,Recommend=@Recommend where Articleid=@Articleid", model);
            }
        }
    }

    public partial class AttentRepository : IAttentRepository
    {
        public int Add(Attent model)
        {
            using (IDbConnection conn = new SqlConnection(BaseDBConfig.ConnectionString))
            {
                return conn.Execute("insert into Attent(CategoryId,Userid,Status) values(@CategoryId,@Userid,@Status)", model);
            }
        }

        public int Delete(Attent model)
        {
            using (IDbConnection conn = new SqlConnection(BaseDBConfig.ConnectionString))
            {
                return conn.Execute("delete from Attent where CategoryId=@CategoryId and Userid=@Userid and Status=@Status", model);
            }
        }

        public List<Attent> GetModel(Expression<Func<Attent, bool>> whereExpression)
        {
            using (IDbConnection conn = new SqlConnection(BaseDBConfig.ConnectionString))
            {
                return conn.Query<Attent>("Select *From Attent").AsQueryable().Where(whereExpression).ToList();
            }
        }

        public int Update(Attent model)
        {
            return 0;
        }
    }

    public partial class CategoryRepository : ICategoryRepository
    {
        public int Add(Category model)
        {
            using (IDbConnection conn = new SqlConnection(BaseDBConfig.ConnectionString))
            {
                return conn.Execute("insert into Category(Parentid,Cname) values(@Parentid,@Cname)", model);
            }
        }

        public int Delete(Category model)
        {
            using (IDbConnection conn = new SqlConnection(BaseDBConfig.ConnectionString))
            {
                return conn.Execute("delete from Category where CategoryId=@CategoryId", model);
            }
        }

        public List<Category> GetModel(Expression<Func<Category, bool>> whereExpression)
        {
            using (IDbConnection conn = new SqlConnection(BaseDBConfig.ConnectionString))
            {
                return conn.Query<Category>("Select *From Category").AsQueryable().Where(whereExpression).ToList();
            }
        }

        public int Update(Category model)
        {
            using (IDbConnection connection = new SqlConnection(BaseDBConfig.ConnectionString))
            {
                return connection.Execute("update Category set Parentid=@Parentid,Cname=@Cname where CategoryId=@CategoryId", model);
            }
        }
    }

    public partial class CommentRepository : ICommentRepository
    {
        public int Add(Comment model)
        {
            using (IDbConnection conn = new SqlConnection(BaseDBConfig.ConnectionString))
            {
                return conn.Execute("insert into Comment([Content],CommDate,Thumbs,Articleid,Userid) values(@Content,@CommDate,@Thumbs,@Articleid,@Userid)", model);
            }
        }

        public int Delete(Comment model)
        {
            using (IDbConnection conn = new SqlConnection(BaseDBConfig.ConnectionString))
            {
                return conn.Execute("delete from Comment where Commentid=@Commentid", model);
            }
        }

        public List<Comment> GetModel(Expression<Func<Comment, bool>> whereExpression)
        {
            using (IDbConnection conn = new SqlConnection(BaseDBConfig.ConnectionString))
            {
                return conn.Query<Comment>("Select *From Comment").AsQueryable().Where(whereExpression).ToList();
            }
        }

        public int Update(Comment model)
        {
            using (IDbConnection connection = new SqlConnection(BaseDBConfig.ConnectionString))
            {
                return connection.Execute("update Comment set [Content]=@Content,CommDate=@CommDate,Thumbs=@Thumbs,Articleid=@Articleid,Userid=@Userid where Commentid=@Commentid", model);
            }
        }
    }

    public partial class UserInfoRespository : IUserInfoRepository
    {
        public int Add(UserInfo model)
        {
            using (IDbConnection conn = new SqlConnection(BaseDBConfig.ConnectionString))
            {
                return conn.Execute("insert into UserInfo(Uname,Upwd,Email) values(@Uname,@Upwd,@Email)", model);
            }
        }

        public int Delete(UserInfo model)
        {
            using (IDbConnection conn = new SqlConnection(BaseDBConfig.ConnectionString))
            {
                return conn.Execute("delete from UserInfo where Userid=@Userid", model);
            }
        }

        public List<UserInfo> GetModel(Expression<Func<UserInfo, bool>> whereExpression)
        {
            using (IDbConnection conn = new SqlConnection(BaseDBConfig.ConnectionString))
            {
                return conn.Query<UserInfo>("Select *From UserInfo").AsQueryable().Where(whereExpression).ToList();
            }
        }

        public int Update(UserInfo model)
        {
            using (IDbConnection connection = new SqlConnection(BaseDBConfig.ConnectionString))
            {
                return connection.Execute("update UserInfo set Uname=@Uname,Upwd=@Upwd,Email=@Email where Userid=@Userid", model);
            }
        }
    }

    public partial class AdminRespository : IAdminRepository
    {
        public int Add(Admin model)
        {
            using (IDbConnection conn = new SqlConnection(BaseDBConfig.ConnectionString))
            {
                return conn.Execute("insert into Admin(AdminName,AdminPwd) values(@AdminName,@AdminPwd)", model);
            }
        }

        public int Delete(Admin model)
        {
            using (IDbConnection conn = new SqlConnection(BaseDBConfig.ConnectionString))
            {
                return conn.Execute("delete from Admin where Adminid=@Adminid", model);
            }
        }

        public List<Admin> GetModel(Expression<Func<Admin, bool>> whereExpression)
        {
            using (IDbConnection conn = new SqlConnection(BaseDBConfig.ConnectionString))
            {
                return conn.Query<Admin>("Select *From Admin").AsQueryable().Where(whereExpression).ToList();
            }
        }

        public int Update(Admin model)
        {
            using (IDbConnection connection = new SqlConnection(BaseDBConfig.ConnectionString))
            {
                return connection.Execute("update Admin set AdminName=@AdminName,AdminPwd=@AdminPwd where Adminid=@Adminid", model);
            }
        }
    }

    public partial class ProblemRespository : IProblemRepository
    {
        public int Add(Problem problem)
        {
            using (IDbConnection conn = new SqlConnection(BaseDBConfig.ConnectionString))
            {
                return conn.Execute("insert into Problem(Pname,PPno) values(@Pname,@PPno)", problem);
            }
        }

        public int Delete(Problem problem)
        {
            using (IDbConnection conn = new SqlConnection(BaseDBConfig.ConnectionString))
            {
                return conn.Execute("delete from Problem where Pno=@Pno", problem);
            }
        }

        public List<Problem> GetModel(Expression<Func<Problem, bool>> whereExpression)
        {
            using (IDbConnection conn = new SqlConnection(BaseDBConfig.ConnectionString))
            {
                return conn.Query<Problem>("Select *From Problem").AsQueryable().Where(whereExpression).ToList();
            }
        }

        public int Update(Problem problem)
        {
            using (IDbConnection connection = new SqlConnection(BaseDBConfig.ConnectionString))
            {
                return connection.Execute("update Problem set Pname=@Pname,PPno=@PPno where Pno=@Pno", problem);
            }
        }
    }
    public partial class WorkOrderRespository : IWorkOrderRepository
    {
        public int Add(WorkOrder workOrder)
        {
            using (IDbConnection conn = new SqlConnection(BaseDBConfig.ConnectionString))
            {
                return conn.Execute("insert into WorkOrder(WOname,Email,Describe,Enclosure,Pno) values(@WOname,@Email,@Describe,@Enclosure,@Pno)", workOrder);
            }
        }

        public int Delete(WorkOrder workOrder)
        {
            using (IDbConnection conn = new SqlConnection(BaseDBConfig.ConnectionString))
            {
                return conn.Execute("delete from WorkOrder where WOno=@WOno", workOrder);
            }
        }

        public List<WorkOrder> GetModel(Expression<Func<WorkOrder, bool>> whereExpression)
        {
            using (IDbConnection conn = new SqlConnection(BaseDBConfig.ConnectionString))
            {
                return conn.Query<WorkOrder>("Select *From WorkOrder").AsQueryable().Where(whereExpression).ToList();
            }
        }

        public int Update(WorkOrder workOrder)
        {
            using (IDbConnection connection = new SqlConnection(BaseDBConfig.ConnectionString))
            {
                return connection.Execute("update Problem set WOname=@WOname,Email=@Email,Describe=@Describe,Enclosure=@Enclosure,Pno=@Pno where WOno=@WOno", workOrder);
            }
        }
    }
}
